﻿using System;
using System.Web.Mvc;
using NCP.Recruiting.ApplicationServices.Interfaces;
using NCP.Recruiting.ApplicationServices.Messaging.Companies;
using NCP.Recruiting.ApplicationServices.ViewModels;
using NCP.Recruiting.WebApp.Authentication;
using NCP.Recruiting.WebApp.Models;

namespace NCP.Recruiting.WebApp.Controllers {
    public class AuthController : Controller {
        private readonly IFormsAuthentication _formsAuthentication;
        private readonly ICompanyAppService _companyAppService;

        public AuthController(IFormsAuthentication formsAuthentication,
            ICompanyAppService userService) {
            this._companyAppService = userService;
            this._formsAuthentication = formsAuthentication;
        }

        public ActionResult Index() {
            var model = new LoginFormModel();

            return View(model);
        }

        public ActionResult SignIn() {
            return View();
        }

        [HttpPost]
        public ActionResult SignIn(LoginFormModel model, string returnUrl) {
            if (ModelState.IsValid) {
                ValidateUserRequest request = new ValidateUserRequest {
                    Username = model.UserName,
                    Password = model.Password
                };

                ValidateUserResponse response = _companyAppService.ValidateUser(request);
                if (response.Success) {
                    var user = response.AuthenticatedUser;
                    _formsAuthentication.SetAuthCookie(this.HttpContext,
                        UserAuthenticationTicketBuilder.CreateAuthenticationTicket(user));

                    if (model.RemberMe)
                    {
                        //_formsAuthentication.SetAuthCookie(model.UserName, model.RemberMe);
                    }
                 
                    if (!String.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl))
                        return Redirect(returnUrl);
                    else
                        return RedirectToAction("Index", "Home");
                }
                else {
                    // Report Message for failing 
                    ViewBag.ErrorMessage = "Username Or Password is incorrect!";
                }
            }

            return View(model);
        }

        public ActionResult SignOut() {
            this._formsAuthentication.Signout();
            return this.RedirectToAction("SignIn", "Auth");
        }

        public ActionResult Register() {
            return View(new RegisterFormModel());
        }

        [HttpPost]
        public ActionResult Register(RegisterFormModel formModel) {
            if (ModelState.IsValid) {
                var response = _companyAppService.RegisterUser(new RegisterUserRequest() { RegisterFormModel = formModel });
                if (response.Success) {
                    var user = response.AuthenticatedUser;
                    _formsAuthentication.SetAuthCookie(this.HttpContext,
                        UserAuthenticationTicketBuilder.CreateAuthenticationTicket(user));

                    return this.RedirectToAction("Index", "Home");
                }
                else {
                    // Report Message for failing 
                    ViewBag.ErrorMessage = "Register Fail!";
                }
            }
          
            return View(formModel);
        }

    }
}
